// const messageUtil = require('../../utils/messageUtil.js')
import messageUtil from "../../utils/messageUtil"
import mobileApi from "../../api/mobileApi"
import commonData from "../../api/data"
import lbkOrderApi from "../../api/lbkOrderApi"
import logApi from "../../api/logApi"
import httpUtil from "../../utils/httpUtil"
import commonApi from "../../api/commonApi"
Component({
  mixins: [],
  data: {
    counter: '',
    orderLocal:"",
    rootImagePath: "https://web.78keji.cn/v2img/new/sh/",
    formData: {
      username: '',
      phone: '',
      code: '',
      isAgreement: false,
      province: "",
      yys: ""
    },
    mobileList: [
      '177***  3分钟前  领取成功',
      '138***  3分钟前  领取成功',
      '181***  3分钟前  领取成功',
      '139***  3分钟前  领取成功',
      '177***  3分钟前  领取成功',
      '138***  3分钟前  领取成功',
      '181***  3分钟前  领取成功',
      '139***  3分钟前  领取成功',

    ],
    isShowAgreement: false,
    agreementType: 1,
    isShowAgreementConfirm: false,
    isShowSmsConfirm: false,
    timeData: {
      lastTime: 0,
      hour: 23,
      minute: 59,
      second: 59,
      milliseconds: 100
    },
    oder: "",
    showTimeIntervalId: 0,
    mobileList: [

      '177***  抢到了',
      '138***  抢到了',
      '181***  抢到了',
      '139***  抢到了',
      '177***  抢到了',
      '138***  抢到了',
      '181***  抢到了',
      '139***  抢到了',
    ]
  },
  props: {
    rootImagePath: '',
    onGoToPay: data => {
      console.log(data)
    }
  },
  didMount() {
    setTimeout(() => {
      let params = {
        // a: commonData.getA(),
        a: '64357a213e104b02376a788abae01bb5',
        proCode: 'LH2509',
        uuid: this.uuid()
      }
      my.httpRequest({
        url: 'https://www.sx2b2c.com/api/tongji/tongji0520',
        data: params,
        method: "POST",
        headers: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success(rex) {
          console.log(rex)
        },
        fail: function (xhr, type, status) {}
      })
    }, 2000);
    // this.$page.keepAgr


  },
  didUpdate() {

  },
  didUnmount() {

  },
  onInit() {



  },
  methods: {


    getAuthPhoneNumber(e) {



    },
    handleAuthError(e) {
      console.log('getPhoneNumber handleAuthError', e)
    },
    agreementConfirmOkHandler() {
      let formData = this.data.formData
      formData.isAgreement = true

      this.setData({
        formData: formData,
        isShowAgreementConfirm: false
      })
      this.submitMethod()
    },
    agreementConfirmCancelHandler() {
      this.setData({
        isShowAgreementConfirm: false
      })
    },
    agreementConfirmShowAgreementHandler(value) {
      let data = {
        target: {
          dataset: {
            attr: value
          }
        }
      }
      this.showAgreementHandler(data)
    },
    bindInput(e) {
      console.log(e)
      if (e.detail.value.length == '11') {
        let formData = this.data.formData
        formData.phone = e.detail.value

        this.setData({
          formData: formData
        })

        this.submitMethod()
      }

    },
    uuid() {
      var s = [];
      var hexDigits = "0123456789abcdef";
      for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
      }
      s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
      s[8] = s[13] = s[18] = s[23] = "-";

      var uuid = s.join("");
      my.setStorageSync({
        key: 'uuid',
        data: uuid
      })

      return uuid;
    },
    re(){
      let count = 0;
     
      setTimeout(() => {
        messageUtil.showLoadings()
       }, 2000);
const intervalId = setInterval(() => {
 
   let params = {

        orderLocal:this.data.orderLocal
      }
      httpUtil.post("/api/souhu/queryOrderByOrderNo", params, res => {
        console.log(res)
      if(res.data.status==2){
        messageUtil.closeLoading()
        my.showToast({
          type: 'success',
          content: '成功',
          duration: 3000,
          success: () => {
      
          },
        });
        this.props.onGoToPay('成功')
       
        clearInterval(intervalId);
        //成功
      }else{
        
      }

      }, err => {
     
     

      })
  count++;
 
  if (count == 4) {
    messageUtil.closeLoading()
    my.showToast({
      type: 'fail',
      content: '支付失败',
      duration: 3000,
      success: () => {
  
      },
    });
    this.props.onGoToPay('支付失败')
    clearInterval(intervalId);
  }
}, 2000);
    
    },
    submitMethod() {
      my.hideKeyboard()
      var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      console.log(this.data.formData.phone)
      if (!(reg_tel.test(this.data.formData.phone))) {
        messageUtil.showFail('手机号格式不正确');
        return;
      }


      // formData.value.isAgreement = true;
      if (!this.data.formData.isAgreement) {
        // messageUtil.showFail('请先同意并阅读相关条款')
        console.log('submitMethod2')
        this.setData({
          isShowAgreementConfirm: true
        })
        return;
      }

      let params = {

        a: '64357a213e104b02376a788abae01bb5',
        mobile: this.data.formData.phone,
        proCode: 'LH2509',
        proType: 3,
        openid:commonData.getOpenid(),
        // openid:'123',
        uuid: my.getStorageSync({
          key: 'uuid'
        }).data
      }
      if (commonData.getParam()) {
        params.param = commonData.getParam()
      }
      messageUtil.showLoading()
      
      httpUtil.post("/api/souhu/subOrder", params, res => {
        console.log(res)
        this.data.orderLocal=res.data.orderLocal

        this.setData({
          orderLocal: this.data.orderLocal
        })
        setTimeout(() => {
          
         this.re()
        }, 3000);
        messageUtil.closeLoading()
        logApi.addKeepLog('版本号' + commonApi.currentVersion + ',下单参数' + JSON.stringify(params) + '。返回：' + JSON.stringify(res))
        let urls = res.data.payUrl
        let url = 'https://ulink.alipay.com/?scheme=alipays%3A%2F%2Fplatformapi%2Fstartapp%3FsaId%3D10000007%26clientVersion%3D3.7.0.0718%26qrcode%3D' +
          encodeURIComponent(encodeURIComponent(urls));
          
        my.ap.openURL({
          url: url, // 请将 url 替换为有效的页面地址
          success: (res) => {
            console.log('openURL success', res)
          },
          fail: (err) => {
            logApi.addKeepLog('版本号' + commonApi.currentVersion + ',下单发生错误，参数' + JSON.stringify(params) + '。返回：' + JSON.stringify(err))
            console.log('openURL success', err)
          }
        });

      }, err => {
     
        messageUtil.closeLoading()

      })
      // my.httpRequest({
      //   url: 'https://www.sx2b2c.com/api/souhu/subOrder',
      //   data: params,
      //   method: "POST",
      //   headers: {
      //     'content-type': 'application/x-www-form-urlencoded' // 默认值
      //   },
      //   success(rex) {
      //     console.log(rex)
      //     let urls=rex.data.data.payUrl
      //     let url ='https://ulink.alipay.com/?scheme=alipays%3A%2F%2Fplatformapi%2Fstartapp%3FsaId%3D10000007%26clientVersion%3D3.7.0.0718%26qrcode%3D' +
      //     encodeURIComponent(encodeURIComponent(urls));
      //     my.ap.openURL({
      //           url:url, // 请将 url 替换为有效的页面地址
      //           success: (res) => {
      //             console.log('openURL success', res)
      //           },
      //           fail: (err) => {
      //             console.log('openURL success', err)
      //           }
      //         });
      //   },
      //   fail: function (xhr, type, status) {}
      // })
      console.log('submit', this.data.formData)
      // mobileApi.getYys(this.data.formData.phone, getYysRes => {
      //   my.setStorageSync({
      //     key: 'mobile',
      //     data: this.data.formData.phone
      //   })
      //   let params = {
      //     // a:commonData.getA(),
      //     a:'64357a213e104b02376a788abae01bb5',
      //     mobile: this.data.formData.phone,
      //     proCode: 'LH2520',
      //     proType: 2,
      //     openid:commonData.getOpenid(),
      //     // openid:'123',
      //     uuid:my.getStorageSync({
      //       key: 'uuid'
      //     }).data

      //   }
      //   if (commonData.getParam()) {
      //     params.param = commonData.getParam()
      //   }

      //   // let query = "mobile=" + this.data.formData.phone

      //   // params.returnUrl = 'http://keep.syhy000.com/h5GotoZfbMini/#/?appid=2021004135681441&url=pages/keeplq/keeplq&query=' + encodeURIComponent(query)
      //   messageUtil.showLoading()
      //   lbkOrderApi.submitOrder(params, res => {
      //     messageUtil.closeLoading()



      //     // my.ap.openURL({
      //     //   url:url, // 请将 url 替换为有效的页面地址
      //     //   success: (res) => {
      //     //     console.log('openURL success', res)
      //     //   },
      //     //   fail: (err) => {
      //     //     console.log('openURL success', err)
      //     //   }
      //     // });

      //     my.paySignCenter({
      //       signStr: res.data.str,
      //       success: (paySignCenterRes) => {
      //         console.log('zhouqiPayHandler success ' + JSON.stringify(paySignCenterRes))
      //         console.log(paySignCenterRes.resultStatus);
      //         if (paySignCenterRes.resultStatus && parseInt(paySignCenterRes.resultStatus) == 6001) {
      //           messageUtil.closeLoading()
      //           messageUtil.showFail('支付失败')
      //           this.props.onGoToPay('支付失败')
      //           return;
      //         } else {
      //           this.props.onGoToPay('成功')
      //         }

      //       },
      //       fail: (paySignCenterResErr) => {
      //         console.log('zhouqiPayHandler fail ' + JSON.stringify(paySignCenterResErr))
      //         this.props.onGoToPay('拉起失败')
      //         logApi.addKeepLog('龙宝卡拉起周期扣款失败：' + JSON.stringify(paySignCenterResErr))
      //       }
      //     });

      //   }, err => {

      //   })
      // })

    },
    inputHandler(e) {
      this.setData({
        formData: {
          phone: e.detail.value
        },
      });
    },
    agreementChange(e) {
      console.log(e)
      let formData = this.data.formData
      formData.isAgreement = !formData.isAgreement
      this.setData({
        formData: formData
      })
    },
    showAgreementHandler(e) {
      this.setData({
        agreementType: parseInt(e.target.dataset.attr),
        isShowAgreement: true
      })
    },
    showAgreementHandlers() {
      let urls = 'https://web.78keji.cn/xieyi/#/'
      let url =
        'https://ulink.alipay.com/?scheme=alipays%3A%2F%2Fplatformapi%2Fstartapp%3FsaId%3D10000007%26clientVersion%3D3.7.0.0718%26qrcode%3D' +
        encodeURIComponent(encodeURIComponent(urls));
      my.ap.openURL({
        url: url, // 请将 url 替换为有效的页面地址
        success: (res) => {
          console.log('openURL success', res)
        },
        fail: (err) => {
          console.log('openURL success', err)
        }
      });
    },

    agreementOkHandler() {
      this.setData({
        isShowAgreement: false
      })
    },
  },
});